package com.bxl.chooser.oper;


import com.bxl.chooser.bean.ChooseMsg;
import com.bxl.chooser.bean.ChooseResponseMsg;
import com.bxl.chooser.context.ChooserContext;
import com.bxl.chooser.oper.impl.*;
import com.bxl.netty.rpc.common.message.PackageMsg;
import lombok.AllArgsConstructor;
import lombok.Getter;

/**
 * <>
 *
 * @author baixl
 * @date 2021/3/25
 */
public enum OperDefine implements IOperSv<ChooseResponseMsg>{
    CHOOSER(1,"选举操作",new ChooserService()),NONE(-1,"未知的操作",null),HEART_BEAT(2,"LEADER心跳",new HeartBeatOperService()),
    ADDMESSAGE(3,"接受消息",new AddMessageService()),COMMITMSG(4,"提交消息",new CommitMessageService())
    ,SYNCMESSAGE(5,"同步消息",new AddMessageService()),CLIENTMESSAGE(7,"提交消息",new AddMessageService()),
    CLIENTQRERY(8,"查询消息",new QueryMessageService());
    private int oper;
    private String desc;
    private IOperSv<ChooseResponseMsg> sv;

    public int getOper() {
        return oper;
    }

    public String getDesc() {
        return desc;
    }

    public IOperSv<ChooseResponseMsg> getSv() {
        return sv;
    }

    OperDefine(int oper, String desc, IOperSv<ChooseResponseMsg> sv) {
        this.oper = oper;
        this.desc = desc;
        this.sv = sv;
    }

    @Override
    public ChooseResponseMsg oper(PackageMsg<ChooseMsg> msg, ChooserContext context) {
        return sv.oper(msg, context);
    }
    
    public static OperDefine getOper(int oper){
        for (OperDefine operDefine : values()) {
            if(oper == operDefine.getOper()){
                return operDefine;
            }
        }
        return NONE;
    }
}
